package com.leanyuan.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leanyuan.main.domain.ForceReceipt;
import com.leanyuan.main.domain.RepReg;
import com.leanyuan.main.service.ForceReceiptService_M;
import com.leanyuan.main.service.RepRegService_M;
import com.leanyuan.main.util.RepformVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/force-receipt")
public class ForceReceiptController {
    @Autowired
    private ForceReceiptService_M forceReceiptServiceM;
    @Autowired
    private RepRegService_M repRegServiceM;

    @PostMapping("addForce")
    public RepformVo addForce(@RequestBody ForceReceipt forceReceipt) {
        System.out.println(forceReceipt);
        forceReceiptServiceM.save(forceReceipt);
        QueryWrapper<RepReg> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id",forceReceipt.getWorkId());
        RepReg repReg = repRegServiceM.getOne(wrapper);
        repReg.setWorkStatu("未回访");
        repRegServiceM.update(repReg,wrapper);
        return RepformVo.success(null);
    }
    @RequestMapping("upload")
    public RepformVo upload(MultipartFile headImgFile) throws IOException {
        System.out.println("******************************************");
        System.out.println(headImgFile);
        if(headImgFile!=null && headImgFile.getSize()>0) {

            //得到绝对路径(业务规则）
//            String realpath = session.getServletContext().getRealPath("upload");
            String realpath=System.getProperty("user.dir")+"\\information-module\\src\\main\\webapp\\upload";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            System.out.println("*******"+realpath+"*******");
            String dir = sdf.format(new Date()); //20210608
            File file = new File(realpath + "\\" + dir);
            if (!file.exists()) {
                file.mkdir();
            }

            //创建一个新的唯一的文件名(业务规则）
            String oldName=headImgFile.getOriginalFilename();
            String name= UUID.randomUUID()+oldName.substring(oldName.lastIndexOf("."));

            //实现文件上传
            headImgFile.transferTo(new File(realpath+"\\"+dir+"\\"+name));
            return RepformVo.success("upload/"+dir+"/"+name);
        }
        return RepformVo.fail(null);
    }
}

